package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.ConsumerMapper;
import com.guigu.pojo.Consumer;
import com.guigu.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Objects;

/**
 * <p>
 * 前台用户表 服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    private ConsumerMapper consumerMapper;


    @Override
    public Consumer selectOneByLoginNameAndPassword(String loginName, String password) {
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name",loginName);
        queryWrapper.eq("password",password);
        return consumerMapper.selectOne(queryWrapper);
    }

    @Override
    public Object loginByPhone(String phone) {
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return consumerMapper.selectOne(queryWrapper);
    }

    @Override
    public Consumer register(Consumer consumer) {
        int i = consumerMapper.insert(consumer);
        if(i>0){
            return consumerMapper.selectById(consumer.getConsumerId());
        }
        return null;
    }

    @Override
    public Integer updatePassword(HashMap<String, Object> map) {
        String oldPassword = (String) map.get("oldPassword");
        String newPassword = (String) map.get("newPassword");
        Integer consumerId = Integer.parseInt((String) map.get("consumerId"));
        Consumer consumer = consumerMapper.selectById(consumerId);
        if(!Objects.equals(consumer.getPassword(), oldPassword)){
            return 1;
        }
        if(Objects.equals(consumer.getPassword(), newPassword)){
            return 2;
        }
        consumer.setPassword(newPassword);
        consumerMapper.updateById(consumer);
        return 0;
    }
}
